package com.blog.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blog.user.entity.User;
import com.blog.user.entity.vo.UserVo;
import com.blog.user.service.UserService;
import com.blog.utils.JWTUtils;
import com.blog.utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zw
 * @since 2021-12-08
 */
@RestController
@RequestMapping("/userInfo")
@Slf4j
@CrossOrigin("*")
public class UserController {
    @Autowired
    private UserService userService;


    //通过ID查询用户
    @GetMapping("/getUserById/{id}")
    public ResponseResult getUserById(@PathVariable  String id,@RequestHeader String token) {
        if(!JWTUtils.checkToken(token)){
            return ResponseResult.error().message("检测到用户未登录或登录信息已过期，请登录后再进行操作");
        }
        UserVo user = userService.getById(id);
        log.info("id"+id+"被查询");
        return ResponseResult.ok().data("user",user);
    }
    //根据登录名查找用户
    @GetMapping("/getUserByName/{userName}")
    public ResponseResult getUserByUserName(@PathVariable  String userName,
                                            @RequestHeader String token) {
        if(!JWTUtils.checkToken(token)){
            return ResponseResult.error().message("检测到用户未登录或登录信息已过期，请登录后再进行操作");
        }
        User user1 = userService.getOneByName(userName);
        if(ObjectUtils.isEmpty(user1))
            return ResponseResult.ok();
        UserVo user = new UserVo(user1);
        user.setRole(userService.getRoleByID(user1.getUserId()));
        log.info("登录名"+userName+"被查询");
        return ResponseResult.ok().data("user",user);
    }

    //分页查询所有用户  page第几页  limit每页几个
    @GetMapping("/getUserPage/{page}/{limit}")
    public ResponseResult  getUserPageList(@PathVariable long page,@PathVariable long limit){
                                           /*String username,@RequestHeader String token){
        if(!JWTUtils.checkToken(username, token)){
            return ResponseResult.error().message("检测到用户未登录或登录信息已过期，请登录后再进行操作");
        }*/
        Page<User> pageInfo=new Page<>(page,limit);
        userService.pageQuery(pageInfo,null);
        List<User> userList1=pageInfo.getRecords();
        long total=pageInfo.getTotal();
        log.info("获取了用户列表"+"第"+page+"页");
        List<UserVo> userList =new ArrayList<UserVo>();
        for (User user:userList1) {
            log.info(user.toString());
            UserVo userVo = userService.getById(user.getUserId());
            userVo.setRole(userService.getRoleByID(user.getUserId()));
            log.info(userVo.toString());
            userList.add(userVo);
        }
        return ResponseResult.ok().data("total",total).data("userList",userList);
    }

    //通过用户昵称模糊查询用户
    @GetMapping("/getUserByPet/{pet}/{page}/{limit}")
    public ResponseResult getUserByPet(@PathVariable String pet,@PathVariable long page,@PathVariable long limit,
                                       @RequestHeader String token){
        if(!JWTUtils.checkToken(token)){
            return ResponseResult.error().message("检测到用户未登录或登录信息已过期，请登录后再进行操作");
        }
        Page<User> pageInfo=new Page<>(page,limit);
        if(pet==null)
            log.info("昵称为空");
        userService.pageQuery(pageInfo,pet);
        long total=pageInfo.getTotal();
        List<User> userList1=pageInfo.getRecords();
        log.info("模糊查询："+pet);
        List<UserVo> userList =new ArrayList<UserVo>();
        for (User user:userList1) {
            log.info(user.toString());
            UserVo userVo = userService.getById(user.getUserId());
            userVo.setRole(userService.getRoleByID(user.getUserId()));
            log.info(userVo.toString());
            userList.add(userVo);
        }
        return ResponseResult.ok().data("total",total).data("userList",userList);
    }


    //通过id查询用户是否被冻结
    @GetMapping("/isFreeze/{id}")
    public ResponseResult getUserByPet(@PathVariable String id,
                                       String username,@RequestHeader String token) {
        if(!JWTUtils.checkToken(username, token)){
            return ResponseResult.error().message("检测到用户未登录或登录信息已过期，请登录后再进行操作");
        }
        Integer isFreeze;
        UserVo user = userService.getById(id);
        if(user==null) {
            log.info("无此用户");
            return ResponseResult.error().message("无此用户");
        }
        isFreeze=user.getUserFreeze();
        log.info(id+"是否被冻结:"+isFreeze.toString());
        return ResponseResult.ok().data("isFreeze",isFreeze);
    }

    //修改用户基本信息
    @PostMapping("/updateUserInfo")
    public ResponseResult updateUserInfo(@RequestBody User userInfo, @RequestHeader String token){
        if(!JWTUtils.checkToken(token)){
            return ResponseResult.error().message("检测到用户未登录或登录信息已过期，请登录后再进行操作");
        }
        UserVo uservo = userService.getById(userInfo.getUserId());
        if(uservo==null) {
            log.info(userInfo.getUserId()+"无此用户");
            return ResponseResult.error().message("无此用户");
        }


        User user=userService.getOneByName(uservo.getUserName());

        if(!ObjectUtils.isEmpty(userInfo.getUserEmail()))
            user.setUserEmail(userInfo.getUserEmail());
        if(!ObjectUtils.isEmpty(userInfo.getUserDescription()))
            user.setUserDescription(userInfo.getUserDescription());
        if(!ObjectUtils.isEmpty(userInfo.getUserGender()))
            user.setUserGender(userInfo.getUserGender());
        if(!ObjectUtils.isEmpty(userInfo.getUserPet()))
            user.setUserPet(userInfo.getUserPet());
        userService.updateById(user);
        log.info(userInfo.getUserId()+"用户信息被修改了:"+userInfo.toString());
        return ResponseResult.ok();
    }

    //用户账号冻结状态修改
    @PostMapping("/updateFreeze")
    public ResponseResult updateFreeze(String id,Integer Freeze){
        UserVo uservo = userService.getById(id);
        if(uservo==null) {
            log.info(id+"无此用户");
            return ResponseResult.error().message("无此用户");
        }
        User user=userService.getOneByName(uservo.getUserName());
        user.setUserFreeze(Freeze);
        userService.updateById(user);
        log.info(id+"用户冻结状态被修改为"+user.getUserFreeze());
        return ResponseResult.ok();
    }

    //
    @PostMapping("/deleteUser")
    public ResponseResult deleteUser(User userInfo,@RequestHeader String token){
        if(!JWTUtils.checkToken(token)){
            return ResponseResult.error().message("检测到用户未登录或登录信息已过期，请登录后再进行操作");
        }
        userService.removeById(userInfo);
        log.info(userInfo.getUserId() + "被删除了");
        return ResponseResult.ok();
    }

    @PostMapping("/setRoleById")
    public ResponseResult setRoleById(String id,Integer role,@RequestHeader String token){
        if(!JWTUtils.checkToken(token)){
            return ResponseResult.error().message("检测到用户未登录或登录信息已过期，请登录后再进行操作");
        }
        userService.setRoleByID(id,role);
        return ResponseResult.ok();
    }
}

